package com.sc.nft.dao;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.TaskParticipationRecordDTO;
import com.sc.nft.dto.TaskRewardListDTO;
import com.sc.nft.entity.SaasUserTask;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.SaasUserWaitingToBeClaimedTaskDTO;
import com.sc.nft.entity.dto.TaskStarRewardListDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.SaasTaskTypeEnum;
import com.sc.nft.mapper.SaasUserTaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 用户任务命中(SaasUserTask)表Dao层
 *
 * @author 王伟成
 * @since 2024-01-11 15:10:00
 */
@Slf4j
@Repository
public class SaasUserTaskDao extends ServiceImpl<SaasUserTaskMapper, SaasUserTask> {


    public Integer countByTaskId(Long taskId, Long userId) {
        LambdaQueryWrapper<SaasUserTask> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(SaasUserTask::getSaasTaskId, taskId);
        lambdaQuery.eq(SaasUserTask::getUserId, userId);
        lambdaQuery.eq(SaasUserTask::getFulfilStatus, Boolean.TRUE);
        return count(lambdaQuery);
    }


    public SaasUserTask getBySaasTask(Long taskId, Long userId) {
        LambdaQueryWrapper<SaasUserTask> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(SaasUserTask::getSaasTaskId, taskId);
        lambdaQuery.eq(SaasUserTask::getUserId, userId);
        lambdaQuery.eq(SaasUserTask::getFulfilStatus, Boolean.FALSE);
        lambdaQuery.eq(SaasUserTask::getPastDue, Boolean.FALSE);
        lambdaQuery.ge(SaasUserTask::getOverTime, DateUtil.date());
        lambdaQuery.orderByDesc(SaasUserTask::getId);
        lambdaQuery.last("limit 1");
        return getOne(lambdaQuery);
    }


    public IPage<SaasUserWaitingToBeClaimedTaskVO> userWaitingToBeClaimedTask(SaasUserWaitingToBeClaimedTaskDTO saasUserTaskParticipatingTaskDTO, UserInfo user) {
        return baseMapper.userWaitingToBeClaimedTask(saasUserTaskParticipatingTaskDTO.toMp(), saasUserTaskParticipatingTaskDTO, user);
    }
    public SaasUserTask getByIdAndUserId(Long id, Long userId) {
        LambdaQueryWrapper<SaasUserTask> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(SaasUserTask::getId, id);
        lambdaQuery.eq(SaasUserTask::getUserId, userId);
        return getOne(lambdaQuery);
    }
    /**
     * 任务类型和用户id获取数据
     *
     * @param taskType
     * @param userId
     * @return
     */
    public List<SaasUserTask> getByTaskTypeAndUserId(SaasTaskTypeEnum taskType, Long userId) {
        LambdaQueryWrapper<SaasUserTask> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(SaasUserTask::getTaskType, taskType);
        lambdaQuery.eq(SaasUserTask::getUserId, userId);
        lambdaQuery.eq(SaasUserTask::getPastDue, Boolean.FALSE);
        return list(lambdaQuery);
    }

    /**
     * 失效掉所有这个id下正在完成中的任务,把他重置
     * @param id
     */
    public void updateBySaasTaskId(Long id) {
        LambdaUpdateWrapper<SaasUserTask> lambdaQuery = Wrappers.lambdaUpdate();
        lambdaQuery.eq(SaasUserTask::getSaasTaskId, id);
        lambdaQuery.set(SaasUserTask::getPastDue, Boolean.TRUE);
        update(lambdaQuery);
    }


    public IPage<TaskRewardListVO> taskRewardList(TaskRewardListDTO taskRewardListDTO) {
        return baseMapper.taskRewardList(taskRewardListDTO.toMp(), taskRewardListDTO);
    }
    public List<SaasUserTaskIdGainAwardVO> getByUserTaskIdGainAward(Long saasTaskId, Long taskAwardId) {
        return baseMapper.getByUserTaskIdGainAward(saasTaskId, taskAwardId);
    }
    public IPage<TaskParticipationRecordVO> taskParticipationRecord(TaskParticipationRecordDTO taskParticipationRecordDTO) {
        return baseMapper.taskParticipationRecord(taskParticipationRecordDTO.toMp(), taskParticipationRecordDTO);
    }
    public IPage<TaskStarRewardListVO> taskStarRewardList(TaskStarRewardListDTO taskStarRewardListDTO) {
        return baseMapper.taskStarRewardList(taskStarRewardListDTO.toMp(), taskStarRewardListDTO);
    }

    public IPage<TaskParticipationRecordVO> taskStarParticipationRecord(TaskParticipationRecordDTO taskParticipationRecordDTO) {
        return baseMapper.taskStarParticipationRecord(taskParticipationRecordDTO.toMp(), taskParticipationRecordDTO);
    }

    public Boolean receiveAward(SaasUserTask saasUserTask) {
        LambdaUpdateWrapper<SaasUserTask> lambdaQuery = Wrappers.lambdaUpdate();
        lambdaQuery.eq(SaasUserTask::getId, saasUserTask.getId());
        lambdaQuery.set(SaasUserTask::getStatus, Boolean.TRUE);
        lambdaQuery.set(SaasUserTask::getGetTime, DateUtil.date());
       return update(new SaasUserTask(),lambdaQuery);
    }


    /**
     * 同步以触发任务的结束时间
     * @param taskId
     * @param taskEnd
     */
    public void modificationTask(Long taskId, Date taskEnd) {
        LambdaUpdateWrapper<SaasUserTask> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(SaasUserTask::getSaasTaskId, taskId);
        lambdaUpdateWrapper.set(SaasUserTask::getOverTime, taskEnd);
        update(new SaasUserTask(), lambdaUpdateWrapper);
    }
}
